package com.wogu.jwa.web.admin;

import java.io.IOException;
import java.util.HashMap;
import java.util.Set;
import java.util.Iterator;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;

import com.wogu.jwa.web.admin.model.Menus;
import com.wogu.jwa.web.admin.classes.Member;

public class Default extends Member {

	public void defaultGet() throws ServletException, IOException {
		int index = 0;
		StringBuilder sb = new StringBuilder();
		StringBuilder sbTop = new StringBuilder();
		HashMap<Integer, Menus.MenuItem> menus = Menus.getInstance().getMenusCache();
		String groupId = String.valueOf((int) session.getAttribute("groupid"));
		Menus.MenuItem mi = menus.get(0);
		Integer[] topIds = mi.getChildren().toArray(new Integer[0]);
		for(int i = 0; i < topIds.length; i++) {
			int id = topIds[i];
			Menus.MenuItem menu = menus.get(id);
			if((menu.groupId.equals("0") || menu.groupId.equals(groupId)) && menu.notShow.equals("0")) {
				Integer ids[] = menu.getChildren().toArray(new Integer[0]);
				if(ids.length > 0) {
					sb.append(getSubMenus(id, ids, i != 0, true, groupId, menus));
				}
				
				sbTop.append("<li><a id=\"topmenu_");
				sbTop.append(id);
				sbTop.append("\" name=\"topmenu\" href=\"javascript:showTopMenu(");
				sbTop.append(id);
				sbTop.append(")\" class=\"menu");
				if(i == 0) {
					sbTop.append(" selected");
				}
				sbTop.append("\"><span>");
				sbTop.append(menu.menuName);
				sbTop.append("</span></a></li>");
			}
		}
		
		request.setAttribute("topmenu", sbTop.toString());
		request.setAttribute("submenu", sb.toString());
		RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/template/index.jsp");
		view.forward(request, response);
	}
	
	public String getSubMenus(int pid, Integer[] ids, boolean hidden, boolean isTop, String groupId, HashMap<Integer, Menus.MenuItem> menus) {
		StringBuilder sb = new StringBuilder();
		sb.append("<ul id=\"container_");
		sb.append(pid);
		sb.append("\" ");
		if(isTop) {
			sb.append(" name=\"container\"");
		}
		sb.append(" class=\"filetree treeview\"");
		if(hidden) {
			sb.append(" style=\"display:none;\"");
		}
		sb.append(">");
		
		String siteRoot = config.getProperty("site_root");
		for(int i = 0; i < ids.length; i++) {
			int id = ids[i];
			Menus.MenuItem menu = menus.get(id);
			if((menu.groupId.equals("0") || menu.groupId.equals(groupId)) && menu.notShow.equals("0")) {
				sb.append("<li class=\"closed ");
				if(menu.isDir.equals("1")) {
					sb.append("expandable");
				}
				if(i == ids.length - 1) {
					sb.append(" last");
				}
				sb.append(" style=\"width:92%;\">");
				sb.append("<div id=\"flag_");
				sb.append(id);
				sb.append("\" class=\"");
				if(menu.isDir.equals("1")) {
					sb.append("hitarea");
				}
				sb.append(" closed-hitarea expandable-hitarea\" onclick=\"javascript:showMenu(");
				sb.append(id);
				sb.append(")\"></div>");
				if(menu.isDir.equals("1")) {
					sb.append("<span class=\"folder\"><a href=\"javascript:showMenu(");
					sb.append(id);
					sb.append(")\">");
					sb.append(menu.menuName);
					sb.append("</a></span>");
				} else {
					sb.append("<span class=\"file\"><a href=\"");
					sb.append(siteRoot);
					sb.append("/admin?c=");
					sb.append(menu.c);
					sb.append("&a=");
					sb.append(menu.a);
					sb.append("\" target=\"right\">");
					sb.append(menu.menuName);
					sb.append("</a></span>");
				}
				
				Integer subIds[] = menu.getChildren().toArray(new Integer[0]);
				if(subIds.length > 0) {
					sb.append(getSubMenus(id, subIds, true, false, groupId, menus));
				}
				sb.append("</li>");
			}
		}
		sb.append("</ul>");
		return sb.toString();
	}
}